from test_queue import RedisQueue,MysqlDb
import threading

import time

# 第一步，查询审批人数量以及审批人id

# 实例化对象
mydb = MysqlDb()


rq = RedisQueue("rq")

lock = threading.Lock()

# 异步出队方法
def doout(id):

	print(id)

	lock.acquire()

	#while 1:

	uid = rq.get_wait(1)

	if uid:

		# 修改字段  将审批人分配给 审批人   set auid = id where id = uid

		sql = " update `user` set `auid` = %s where `id` = %s " % (id,uid[1])

		print(sql)

		mydb.update(sql) 

		# time.sleep(1)

		print(uid[1])

	lock.release()

# 直接实例化线程实例


def dojob_threads():

	# 查询审批人
	res = mydb.read_list(" select id from `user` where type = 2 order by rand() ")
	print(res)

	threads = []

	for x in res:

		t = threading.Thread(target=doout,args=(x["id"],))
		threads.append(t)

	# 执行批量消费

	[x.start() for x in threads]
	[x.join() for x in threads]



# 轮询任务

def loopjob():

	while 1:

		dojob_threads()

		time.sleep(30)


loopjob()

#dojob_threads()
